Diego Diaz Gomez About Fab Academy Contact

WEEK 13: Networking and Communications

This week was about communication between devices, either wired or wireless.
We got to know different ways of communication, like serial, I2C, radio, bluetooth, wi-fi and more.

Group assignment:

For the group assignment we had to send a message between two projects.
To do so, I worked together with Denise where we tried to communicate our ATtinys 1614 via I2C.


What we did was flashing the two ATtinys, one as a Master (reciever) and another as a Slave(sender), so that the reciever requests certain information and the sender gives it to the Master.
The code for the Master (reciever):

  #include <wire.h>

  void setup() {
    Wire.begin();        // join i2c bus (address optional for master)
    Serial.begin(9600);  // start serial for output
    Serial.println("start");
    delay(1000);
  }

  void loop() {
    Serial.println("first req");
    Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8
    Serial.println("requesting");

    while (Wire.available()) { // slave may send less than requested
      Serial.println("reading");
      char c = Wire.read();    // receive a byte as character
      Serial.print(c);         // print the character
    }

    delay(500);
  }

The code for the Slave (sender):

  #include <Wire.h>

  void setup() {
        Wire.begin(8);                // join i2c bus with address #8
        Wire.onRequest(requestEvent); // register event
    }
  void loop() {
        delay(100);
    }

    // function that executes whenever data is requested by master
    // this function is registered as an event, see setup()
  void requestEvent() {
        Wire.write("diego "); // respond with message of 6 bytes
        // as expected by master

So we flashed both ATTinys with the code and we connected them through serial port to our computers, but when we tried to read the information we were sending, we were having several issues because the comunication was not fluid at all and it worked just a couple of times for some seconds. Then we tried to do the same with an Arduino UNO and it seamed to work well:

After the whole afternoon of tests, we gave up at least for the moment. Then, afterwards, Edu told us that it would be highly recommended to put a 10k pull up resistor in both the SDA and SCL connections. We also fond out later, also thanks to Edu's advise, that there is another library that might work better for I2C communications: https://github.com/technoblogy/tiny-mega-i2c

Individual assignment

This week I made a Barduino with an ESP32 in order to communicate with it in different ways. I used the Barduino 2.0 designed in FabLab Barcelona, (you can check it here), and I redesigned it a bit to make it a bit cleaner as I like it.
Basically, what I did was rearranging all the components to align them and making all the traces straight and perpendicular. I used the copper of the back side of the PCB as ground, making vias where I needed, so that I simplified a lot the design because I was able to get ground wherever I want. I also changed the connectors, because in the original one they were through hole and in the back side, and I wanted them in the front side of the board, that's why I changed them for the ones that can be soldered in the surface of the board.

I milled all the copper to make a clean board with only the traces, using the technique described in the electronics design week.

And the final result was a very clean and neat board:

First of all, before start making tests, I had to install the ESP32 in Arduino IDE:

I used the following configuration, choosing the ESP32 Dev Module, and leaving the other parameters as they were:

Serial:

To begin, I made the most basic example of serial communication, where I just send a message through serial port to the computer:

  #include <Arduino.h>

  void setup() {
      Serial.begin(9600);

      while (!Serial) {
          ; // wait for serial port to connect. Needed for native USB port only
      }
  }

  void loop(){
      Serial.println("Hello Fabacademy");
  }

And it worked:

I2C scanning:

The first test with I2C was to scan some of the sensors that I had used in the imput devices week.
I used the code given in the fabacademy site:

  //
  // hello.I2C.ino
  //
  // I2C hello-world scanner
  //
  // Neil Gershenfeld 12/8/19
  //
  // This work may be reproduced, modified, distributed,
  // performed, and displayed for any purpose, but must
  // acknowledge this project. Copyright is retained and
  // must be preserved. The work is provided as is; no
  // warranty is provided, and users accept all liability.
  //

  #include <Wire.h>

  void setup() {
     Serial.begin(115200);
     Wire.begin();
     }

  uint8_t address,count;

  void loop() {
     count = 0;
     Serial.println("start scan ...");
     for (address = 1; address < 127; address++) {
        Wire.beginTransmission (address);
        if (Wire.endTransmission () == 0) {
           count += 1;
           Serial.print("   found ");
           Serial.print(address,DEC);
           Serial.print(" (0x");
           Serial.print(address,HEX);
           Serial.println (")");
           }
        }
     if (count == 0)
        Serial.println("   no devices found");
     delay(1000);
     }

And I flashed it in my ESP32.

Then I plugged the two temperature sensors, and I was able to see the address of both of them

I2C communication:

Then I decided to test I2C communication between the ESP32 and the ATTiny 1614 board that I made for the input devices week:

To connect them, I had to found the I2C (SDA and SCL) pins of both of my microcontrollers. In the begining I was not able to find the ports in the datasheet of the ESP32, but then I found a website where they say wich pins they are. Afterwards a classmate told me that it might be because you can select which pins you want to use.

Pinout for the ATTiny 1614, where the SDA and SCL pins are 8 and 9:

Pinout for the ESP32, where the SDA and SCL pins are 33 and 36:



So I connected the ESP32 to the computer via FTDI and this one with the ATTiny with the two I2C wires, giving also VCC and GROUND to it.

I configured the Master (ESP32) as receiver, where it requests information.

Code for Master / receiver (ESP32):

  #include <Wire.h>

  void setup() {
      Wire.begin();        // join i2c bus (address optional for master)
      Serial.begin(9600);  // start serial for output
  }

  void loop() {
      Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

      while (Wire.available()) { // slave may send less than requested
          char c = Wire.read();    // receive a byte as character
          Serial.print(c);         // print the character
      }

      delay(500);
  }

Code for sender (ATTiny 1614):

  #include <Wire.h>

  void setup() {
      Wire.begin(8);                // join i2c bus with address #8
      Wire.onRequest(requestEvent); // register event
  }

  void loop() {
      delay(100);
  }

  // function that executes whenever data is requested by master
  // this function is registered as an event, see setup()
  void requestEvent() {
      Wire.write("diego "); // respond with message of 6 bytes
      // as expected by master
  }

And it woked:

Bluetooth:

As the ESP32 has also bluetooth, I wanted to test the connection from my phone.
I used nRF Connect in my iPhone to do so, but it was not recognising my ESP32, still don't know why. I'll give it another chance.
As the connection from my phone was not working, I decided to try to connect from my laptop, so I search for bluetooth devices and I found it:

I used Tera Term to talk with it. After selecting the proper bluetooth port, I was able to send messages via bluetooth between my laptop and the ESP32 that was connected also to the laptop via serial port.

The code I used to activate the bluetooth and send messages, is the one in the Fabacademy page:

  //
  // hello.ESP32-WROOM.SPP.ino
  //
  // ESP32 Bluetooth Serial Port Protocol hello-world
  //
  // Neil Gershenfeld 11/13/19
  //
  // This work may be reproduced, modified, distributed,
  // performed, and displayed for any purpose, but must
  // acknowledge this project. Copyright is retained and
  // must be preserved. The work is provided as is; no
  // warranty is provided, and users accept all liability.
  //

  #include "BluetoothSerial.h"

  BluetoothSerial SerialBT;

  void setup() {
    Serial.begin(115200);
    SerialBT.begin("hello.ESP32-WROOM.SPP");
    printf("\nBluetooth ready for pairing and serial communication\n\n");
    }

  void loop() {
    if (Serial.available())
      SerialBT.write(Serial.read());
    if (SerialBT.available())
      Serial.write(SerialBT.read());
    }

After the bluetooth was available for pairing and communication,

I was able to talk with it:

Files:

Modified Barduino KiCad files
PNG for the traces
PNG for the fill
PNG for the outcut
Gitlab of FabLab Bcn with Barduino files